<template>
  <div :id="id" class="chart-box"/>
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    id: {
      type: String,
      default: 'score-chart'
    },
    data: {
      type: Array,
      default () {
        return () => []
      }
    },
    title: {
      type: String,
      default: '近30天走势'
    }
  },
  data () {
    return {
      chart: null
      // data: []
    }
  },
  created () {
  },
  mounted () {
    // setInterval(() => {
    //   this.initChart()
    // }, 2000)
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', () => {
      this.chart.resize()
    })
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
          text: this.title,
          textStyle: {
            color: '#FFFFFF',
            fontSize: 14
          },
          left: 10,
          top: 10
        },
        color: ['#35A0FC', '#3CD5A7', '#F35968'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              width: 10,
              color: '#172758'
            },
            z: 1
          },
          backgroundColor: 'rgba(37, 69, 125, 0.75)',
          textStyle: {
            color: '#fff'
          },
          borderWidth: 0
        },
        legend: [
          {
            left: '60%',
            top: 10,
            icon: 'circle',
            itemWidth: 8,
            itemHeight: 8,
            textStyle: {
              color: '#35A0FC'
            },
            data: ['检测数', '正常数', '超重数']
          }
        ],
        xAxis: [
          {
            type: 'time',
            minInterval: 3600 * 24 * 1000,
            axisLine: {
              lineStyle: {
                color: '#273B74'
              }
            },
            axisLabel: {
              textStyle: {
                color: '#598FE6'
              }
            },
            splitLine: {
              show: false // 清除线
            }
          }
        ],
        yAxis: [
          {
            boundaryGap: false,
            type: 'value',
            axisLine: {
              show: false
            },
            axisLabel: {
              textStyle: {
                color: '#598FE6',
                fontSize: 14
              }
            },
            splitLine: {
              lineStyle: {
                color: '#273B74'
              }
            }
          }
        ],
        series: [
          {
            name: '检测数',
            type: 'line',
            showSymbol: false,
            lineStyle: {
              // color: '#35A0FC',
              width: 3
            },
            data: this.data[0]
          },
          {
            name: '正常数',
            type: 'line',
            showSymbol: false,
            lineStyle: {
              // color: '#35A0FC',
              width: 3
            },
            data: this.data[1]
          },
          {
            name: '超重数',
            type: 'line',
            showSymbol: false,
            lineStyle: {
              // color: '#35A0FC',
              width: 3
            },
            data: this.data[2]
          }
        ]
      })
      window.addEventListener('resize', () => {
        if (this.chart) {
          this.chart.resize()
        }
      })
    }
  }
}
</script>

<style scoped>
.chart-box {
  height: 100%;
  width: 100%;
}
</style>
